import blogtest.InitAndEnd;
import org.junit.jupiter.api.*;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class HuyaTest {
    static WebDriver webDriver;
    @BeforeAll
   static void init(){
        webDriver = new ChromeDriver();
    }
    @AfterAll
    static void shutdown(){
        webDriver.quit();
    }
    @Test
    @Disabled
    void test01() throws InterruptedException {
        webDriver.get("https://www.huya.com/lpl");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);

        sleep(3000);
        Alert alert = webDriver.switchTo().alert();
        alert.dismiss();
//        webDriver.findElement(By.className(
//                "#J_duyaHeaderRight > div > div.Nav--2HPhApT-QQWPnPIHoPgi7f > div:nth-child(3) > a"))
//                .click();
        webDriver.findElement(By.cssSelector("#duya-header > div > div > div.duya-header-nav > div:nth-child(2) > a"))
                .click();
        //验证直播url
        String cur_handle = "";
        Set<String>  handles = webDriver.getWindowHandles();
        for (String x:
             handles) {
            cur_handle = x;
        }
        Assertions.assertEquals("https://www.huya.com/l",cur_handle);
    }
    /*
            验证登录正常
     */
    @Test
    @Order(1)
    void test02() throws InterruptedException {
        webDriver.get("http://124.221.91.27:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#username")).sendKeys("lisi");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(2000);
        Alert alert = webDriver.switchTo().alert();
        alert.accept();
       // webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        sleep(2000);
        String cur_username = webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user")).
                getText();
        Assertions.assertEquals("lisi",cur_username);
    }
    @Test
    @Order(2)
    void test03() throws InterruptedException {
        webDriver.findElement(By.xpath("//*[@id=\"session-list\"]/li[1]/h3")).click();
        sleep(2000);
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea"))
                .sendKeys("你好");
        sleep(1000);
        webDriver.findElement(By.cssSelector("#submitButton")).click();

    }

}
